package me.zhengjie.modules.inspection.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.inspection.domain.InspectionItem;
import me.zhengjie.modules.inspection.service.InspectionItemService;
import me.zhengjie.modules.inspection.service.dto.InspectionItemQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author xuyukun
* @date 2019-07-02
*/
@RestController
@RequestMapping("api")
public class InspectionItemController {

    @Autowired
    private InspectionItemService inspectionItemService;

    @Log("查询InspectionItem")
    @GetMapping(value = "/inspectionItem")
    @PreAuthorize("hasAnyRole('ADMIN','INSPECTIONITEM_ALL','INSPECTIONITEM_SELECT')")
    public ResponseEntity getInspectionItems(InspectionItemQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(inspectionItemService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增InspectionItem")
    @PostMapping(value = "/inspectionItem")
    @PreAuthorize("hasAnyRole('ADMIN','INSPECTIONITEM_ALL','INSPECTIONITEM_CREATE')")
    public ResponseEntity create(@Validated @RequestBody InspectionItem resources){
        return new ResponseEntity(inspectionItemService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改InspectionItem")
    @PutMapping(value = "/inspectionItem")
    @PreAuthorize("hasAnyRole('ADMIN','INSPECTIONITEM_ALL','INSPECTIONITEM_EDIT')")
    public ResponseEntity update(@Validated @RequestBody InspectionItem resources){
        inspectionItemService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除InspectionItem")
    @DeleteMapping(value = "/inspectionItem/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','INSPECTIONITEM_ALL','INSPECTIONITEM_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        inspectionItemService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
    @Log("根据objectid查")
    @GetMapping(value = "/inspectionItem/byObjectId/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','DICT_ALL','DICT_SELECT')")
    public ResponseEntity getDictsByNmae(@PathVariable  int  id){
        return new ResponseEntity(inspectionItemService.findByObjectId(id),HttpStatus.OK);
    }
}